package com.jxl.coreplay.managers
{
	import com.jxl.coreplay.constants.VideoConstants;
	import com.jxl.coreplay.events.VideoEvent;
	import com.jxl.coreplay.vo.StreamConfigurationVO;
	
	public class StreamingStreamManager extends AbstractStreamManager
	{
		public function StreamingStreamManager(videoType:StreamConfigurationVO):void
		{
			super(videoType);
			
			if(videoType.type != VideoConstants.STREAMING)
			{
				throw new Error("StreamingStreamManager can only handle non-live, non-progressive video.");
				return;
			}
			
			initStreamingVideo();
		}
		
		protected function initStreamingVideo():void
		{
			if(corePlayNetConnection != null)
			{
				if(corePlayNetConnection.connected == true)
				{
					createNetStream(corePlayNetConnection.netConnection);
					_videoType.video.attachNetStream(corePlayNetStream);
					if(_videoType.autoPlay == true)
					{
						playStreamingVideo();
					}
				}
				else
				{
					connect();
				}
			}
			else
			{
				connect();
			}
		}
		
		protected override function onConnectSuccess(event:VideoEvent):void
		{
			initStreamingVideo();
		}
		
		protected function playStreamingVideo():void
		{
			if(corePlayNetStream != null && corePlayNetConnection != null)
			{
				if(corePlayNetConnection.connected == true)
				{
					corePlayNetStream.play(_videoType.streamName, 0);
				}
			}
		}
		
		protected function pauseStreamingVideo():void
		{
			if(corePlayNetStream != null && corePlayNetConnection != null)
			{
				if(corePlayNetConnection.connected == true)
				{
					corePlayNetStream.pause();
				}
			}
		}
		
		protected function stopStreamingVideo():void
		{
			_videoType.video.attachNetStream(null);
			destroyNetStream();
		}
		
		protected function seekStreamingVideo(offset:Number):void
		{
			if(corePlayNetStream != null && corePlayNetConnection != null)
			{
				if(corePlayNetConnection.connected == true)
				{
					corePlayNetStream.seek(offset);
				}
			}
		}
		
		public override function play():void
		{
			playStreamingVideo();
		}
		
		public override function pause():void
		{
			pauseStreamingVideo();
		}
		
		public override function stop():void
		{
			stopStreamingVideo();
		}
		
		public override function seek(offset:Number):void
		{
			seekStreamingVideo(offset);
		}
		
		
	}
}